clear
do "...\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3


********************************************************************************
** OTHER PCP outcomes - TABLE D10
********************************************************************************

****** Experience
xtile experience=mean_age, n(3)

cap drop help
cap drop interaction
cap drop interaction2

g help=(experience==3) if timing==1
bys pnr: egen help_age=max(help)

g interaction=help_age
replace interaction=0 if post==0

g interaction2=low_ses*help_age

******

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_age==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_age low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", replace tex label keep(help_age interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Most experience)


****** Gender

cap drop help
cap drop interaction
cap drop interaction2

g help=(mean_male==0) if timing==1
bys pnr min_year: egen help_male=max(help)

g interaction=help_male
replace interaction=0 if post==0

g interaction2=low_ses*help_male

******

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_male==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_male low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", append tex label keep(help_male interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Male)

****** Ethnic
cap drop help
cap drop interaction
cap drop interaction2 

g help=(mean_dk==0) if timing==1
bys pnr min_year: egen help_dk=max(help)

g interaction=help_dk
replace interaction=0 if post==0

g interaction2=low_ses*help_dk

******

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_dk==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help_dk low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", append tex label keep(help_dk interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Ethnic Danish)

*** UNIVERSITY : UCPH
cap drop help
cap drop interaction
cap drop interaction2

g help=(yder_ku==1) if  timing==1
bys pnr min_year: egen help_ku=max(help)

g interaction=help_ku
replace interaction=0 if post==0

g interaction2=low_ses*help_ku

******

sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_ku==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_ku low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", append tex label keep( help_ku interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(UCPH)


*****  GRADES
sum mean_grades, d
xtile grades=mean_grades, n(3)

replace grades = . if mean_grades==.
g missing_GPA=(grades==. & timing==1)
bys pnr min_year: egen help_grades_missing=max(missing_GPA)
tab help_grades_missing

cap drop help
cap drop interaction
cap drop interaction2

g help=(grades==3) if timing==1
bys pnr min_year: egen help_grades=max(help)

g interaction=help_grades
replace interaction=0 if post==0

g interaction2=low_ses*help_grades


******

sum death if post==1 & help_grades_missing==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1 & help_grades_missing==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help3==0 & help_grades_missing==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help_grades low_ses interaction2 $patient_X $gp_X if post==1 &help_grades_missing==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", append tex label keep(help_grades interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Highest GPA)


*****  Share of low SES patients in year-1
** Merge new GPs share of low SES patients in the year before the patients joins the clinic

rename ydernr yder_temp
rename new_gp ydernr
rename year year_temp
rename min_year year

replace year=year-1

merge m:1 ydernr year using  "$work\mean_ses_yder.dta"
drop if _merge==2
drop _merge

replace year=year+1

rename ydernr new_gp
rename yder_temp ydernr
rename year min_year
rename year_temp year


sum mean_low_ses_ydernr, d
replace mean_low_ses_ydernr=. if !inrange(mean_low_ses_ydernr, `r(p1)', `r(p99)')

cap drop patient_pool
xtile patient_pool=mean_low_ses_ydernr, n(3)

cap drop help
cap drop interaction
cap drop interaction2 

g help=(patient_pool==2)
bys pnr min_year: egen help_patient_pool=max(help)

g interaction=help_patient_pool
replace interaction=0 if post==0

g interaction2=low_ses*help_patient_pool


******


sum death if post==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_patient_pool==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death help_patient_pool low_ses interaction2 $patient_X $gp_X if post==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD10.doc", append tex label keep(help_patient_pool interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Most low-SES patients)


********************************************************************************
** TABLE D11

cap drop help
cap drop interaction
cap drop interaction2

g interaction=help3
replace interaction=0 if post==0

g interaction2=low_ses*help3
********************************************************************************


cap drop experienced
cap drop d_experienced
g experienced=(mean_age>45) if timing==1
bys pnr: egen d_experienced=max(experienced)

* Most experienced
sum death if post==1 & d_experienced==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1  & d_experienced==1
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_age==0  & d_experienced==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_experienced==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Most experienced)


* Least experienced
sum death if post==1 & d_experienced==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if post==1 & low_ses==1  & d_experienced==0
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & help_age==0  & d_experienced==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_experienced==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Least experienced)


* GENDER
cap drop gender
cap drop d_gender
g gender=(mean_male>0.5) if timing==1
bys pnr: egen d_gender=max(gender)

** Most male
sum death if d_gender==1 , d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_gender==0, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_gender==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') ctitle(Most male)

* Least male
sum death if d_gender==1 , d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_gender==1 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_gender==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') ctitle(Least male)



*** ETHNIC
cap drop ethnic
cap drop d_ethnic
g ethnic=(mean_dk<1) if timing==1
bys pnr: egen d_ethnic=max(ethnic)

* Non-ethnic Danish
sum death if d_ethnic==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_ethnic==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_ethnic==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean')  ctitle(Non-ethnic Danish)

* Ethnic Danish
sum death if d_ethnic==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_ethnic==1 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)
reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_ethnic==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') ctitle(ethnic Danish)


* UCPH
cap drop ku2
cap drop d_ku
g ku2=(yder_ku==1) if timing==1
bys pnr: egen d_ku=max(ku2)

* UCPH
sum death if d_ku==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_ku==0 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)
reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_ku==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') ctitle(UCPH)

* Non UCPH
sum death if d_ku==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

ttest death if post==1 & d_ku==1 , by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death help3 low_ses interaction2 $patient_X $gp_X if post==1  & d_ku==0, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableD11.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Gradient  for high SES physicians, `mean0', Outcome mean,`mean') ctitle(Non UCPH)





